package activity;

import flyingpig.com.R;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ToggleButton;

public class SettingsActivity extends Activity {

	private int difficultySetting;
	private String username;
	private EditText editUserName;
	private ToggleButton easyButton;
	private ToggleButton normalButton;
	private ToggleButton hardButton;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settings);
		editUserName = (EditText) findViewById(R.id.editUserName);
		SharedPreferences keyValues = getSharedPreferences("usernameAndDifficulty", Context.MODE_PRIVATE);
		editUserName.setText(keyValues.getString("username", "J. Doe"));
		easyButton = (ToggleButton) findViewById(R.id.easy_button);
		easyButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				difficultySetting = 3;
				easyButton.setButtonDrawable(R.drawable.easy_button_pressed);
				normalButton.setButtonDrawable(R.drawable.normal_button);
				hardButton.setButtonDrawable(R.drawable.hard_button);
			}
		});
		normalButton = (ToggleButton) findViewById(R.id.normal_button);
		normalButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				difficultySetting = 2;
				easyButton.setButtonDrawable(R.drawable.easy_button);
				normalButton.setButtonDrawable(R.drawable.normal_button_pressed);
				hardButton.setButtonDrawable(R.drawable.hard_button);

			}
		});
		hardButton = (ToggleButton) findViewById(R.id.hard_button);
		hardButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				difficultySetting = 1;
				easyButton.setButtonDrawable(R.drawable.easy_button);
				normalButton.setButtonDrawable(R.drawable.normal_button);
				hardButton.setButtonDrawable(R.drawable.hard_button_pressed);
			}
		});
		Button backButton = (Button) findViewById(R.id.difficulty_backButton);
		backButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				saveSettings();
				finish();
			}
		});
	}



	@Override
	public void onBackPressed() {
		saveSettings();
		finish();
	}


	private void saveSettings(){
		SharedPreferences keyValues = getBaseContext().getSharedPreferences("usernameAndDifficulty", Context.MODE_PRIVATE);
		SharedPreferences.Editor keyValuesEditor = keyValues.edit();
		keyValuesEditor.putString("username", editUserName.getText().toString());
		keyValuesEditor.putInt("difficultySetting", difficultySetting);
		keyValuesEditor.commit();
	}
}
